using hao_Common.Response.Models;

namespace hao_Common.Response.Builders
{
    /// <summary>
    /// API响应构建器
    /// </summary>
    public static class ApiResponseBuilder
    {
        #region 成功响应

        /// <summary>
        /// 创建成功响应（无数据）
        /// </summary>
        public static ApiResponse Success(string message = "操作成功")
        {
            return new ApiResponse
            {
                Code = 200,
                Message = message
            };
        }

        /// <summary>
        /// 创建成功响应（带数据）
        /// </summary>
        public static ApiResponse<T> Success<T>(T data, string message = "操作成功")
        {
            return new ApiResponse<T>
            {
                Code = 200,
                Message = message,
                Data = data
            };
        }

        /// <summary>
        /// 创建分页成功响应
        /// </summary>
        public static PagedApiResponse<T> PagedSuccess<T>(IEnumerable<T> data, int pageNumber, int pageSize, int totalCount, string message = "查询成功")
        {
            return new PagedApiResponse<T>
            {
                Code = 200,
                Message = message,
                Data = data,
                PageNumber = pageNumber,
                PageSize = pageSize,
                TotalCount = totalCount
            };
        }

        #endregion

        #region 失败响应

        /// <summary>
        /// 创建失败响应
        /// </summary>
        public static ApiResponse Fail(string message, string? errorCode = null, int code = 400)
        {
            return new ApiResponse
            {
                Code = code,
                Message = message,
                ErrorCode = errorCode
            };
        }

        /// <summary>
        /// 创建失败响应（泛型）
        /// </summary>
        public static ApiResponse<T> Fail<T>(string message, string? errorCode = null, int code = 400)
        {
            return new ApiResponse<T>
            {
                Code = code,
                Message = message,
                ErrorCode = errorCode
            };
        }

        /// <summary>
        /// 创建验证失败响应
        /// </summary>
        public static ApiResponse ValidationFail(string message = "参数验证失败")
        {
            return new ApiResponse
            {
                Code = 400,
                Message = message,
                ErrorCode = "VALIDATION_ERROR"
            };
        }

        /// <summary>
        /// 创建未授权响应
        /// </summary>
        public static ApiResponse Unauthorized(string message = "未授权访问")
        {
            return new ApiResponse
            {
                Code = 401,
                Message = message,
                ErrorCode = "UNAUTHORIZED"
            };
        }

        /// <summary>
        /// 创建禁止访问响应
        /// </summary>
        public static ApiResponse Forbidden(string message = "禁止访问")
        {
            return new ApiResponse
            {
                Code = 403,
                Message = message,
                ErrorCode = "FORBIDDEN"
            };
        }

        /// <summary>
        /// 创建资源未找到响应
        /// </summary>
        public static ApiResponse NotFound(string message = "资源未找到")
        {
            return new ApiResponse
            {
                Code = 404,
                Message = message,
                ErrorCode = "NOT_FOUND"
            };
        }

        /// <summary>
        /// 创建服务器错误响应
        /// </summary>
        public static ApiResponse ServerError(string message = "服务器内部错误")
        {
            return new ApiResponse
            {
                Code = 500,
                Message = message,
                ErrorCode = "SERVER_ERROR"
            };
        }

        #endregion
    }
}